//
//  SaveSkillsVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/4/17.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "SaveSkillsVC.h"
#import "SaveSkillsTableViewCell.h"
#import "AnswersListModel.h"
#import "NSString+Expand.h"
#import "MyServiceVC.h"

@interface SaveSkillsVC ()
<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;
@end

@implementation SaveSkillsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"设置收费金额";
    self.arrData = [NSMutableArray arrayWithArray:self.skillsArr];
    [self loadTabbleView];
}
/*
 方法描述:
 加载列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadTabbleView{
    [self.tableView registerNib:[UINib nibWithNibName:@"SaveSkillsTableViewCell" bundle:nil] forCellReuseIdentifier:@"SaveSkillsTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
#pragma mark ------tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.skillsArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *identifier = @"SaveSkillsTableViewCell";
    SaveSkillsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    AnswersListModel *model = self.skillsArr[indexPath.row];
    cell.titleLabel.text = model.skillName;
    cell.priceTextField.text = [NSString stringWithFormat:@"%.2f",model.price];
    cell.priceTextField.tag = indexPath.row;
    [cell.priceTextField addTarget:self action:@selector(priceValueChange:) forControlEvents:UIControlEventEditingChanged];
    cell.priceTextField.delegate = self;
    return cell;
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([textField.text floatValue] == 0) {
        textField.text =@"";
    }
}
-(void)priceValueChange:(UITextField *)sender{
    
    AnswersListModel *model = self.skillsArr[sender.tag];
    model.price = [sender.text floatValue];
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{

}
- (IBAction)saveClick:(id)sender {
    
    NSMutableArray *arrData = [NSMutableArray array];
    for (NSInteger i = 0; i<self.skillsArr.count; i++) {
        AnswersListModel *model = self.skillsArr[i];
        if (model.price == 0) {
            ALERT_VIEW(@"金额不能为0");
            return;
        }
        if (model.incumbentQuestionId != 0) {
            NSDictionary *dic = @{@"skillId":[NSNumber numberWithInteger:model.skillId],@"price":[NSNumber numberWithFloat:model.price],@"incumbentQuestionId":[NSNumber numberWithInteger:model.incumbentQuestionId]};
            [arrData addObject:dic];
        }else{
            NSDictionary *dic = @{@"skillId":[NSNumber numberWithInteger:model.skillId],@"price":[NSNumber numberWithFloat:model.price]};
            [arrData addObject:dic];
        }
    }

//   NSString *parameters = [NSString dictionaryToJson:arrData];
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"params":arrData} url:@"question/updateIncumbentQuestion" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            for (UIViewController *vc in ((YJMNavigationController *)self.navigationController.navigationController).yjm_viewControllers) {
                if ([vc isKindOfClass:[MyServiceVC class]]) {
                    [self.navigationController popToViewController:vc animated:YES];
                    break;
                }
            }

        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
